<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use app\admin\model\Invitation as InvitationMdl;
use app\admin\model\Member;
use app\admin\model\IntegralGet;
use app\admin\model\Coupon;
use app\admin\model\Membercoupon;
use app\common\BaseHelper;

class Invitation extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function getDetail()
    {
        $param = self::getHttpParam();
        if (empty($param->phone)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'手机号为空');
        }
        $InvitationMdl = new InvitationMdl();
        $fields = 'a.id,a.name as activity_name,img_url,invitation_rules,c.name,c.coupon_type,c.end_time,c.cash_amount,c.discount_strength,c.threshold ';
        $result = $InvitationMdl->alias('a')
            ->join('coupon c','a.invitation_coupon_id = c.id')
            ->field($fields)
            ->find();


        $couponMdl = new Coupon();
        $result = $couponMdl->formatCoupon($result);
        if (!$result){
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $InvitationMdl->getError());
        }
        if (!empty($result['cash_amount'])){
            $result['cash_amount'] = $result['cash_amount'] / 100;
        }
        if (!empty($result['threshold'])){
            $result['threshold'] = $result['threshold'] / 100;
        }
        $result['invitation_rules'] = json_decode($result['invitation_rules'], true);
        //查询该用户已邀请多少人
        $memberMdl = new Member();
        $member = $memberMdl->where(['chief_user_account'=>$param->phone])->count();
        $result['memberCount'] = $member;
        //循环json字符串
        $invitation_rules = (array)$result['invitation_rules'];
        $IntegralGetMdl = new IntegralGet();
        $isGet = $IntegralGetMdl->where(['phone'=>$param->phone])->field('key')->select();
        foreach($invitation_rules as $k => &$v)
        {
            if ((int)$v['member'] - (int)$member <= '0' ){
                $v['status'] = 0;
            }else if((int)$v['member'] - (int)$member > 0){
                $v['status'] = (int)$v['member'] - (int)$member;
            }
            //判断是否已领取 1已领取 0可领取
            if (empty($isGet)){
                $v['isGet'] = 0;
            }
            foreach($isGet as $val){
                if ($k == $val['key']){
                    $v['isGet'] = 1;
                }else{
                    $v['isGet'] = 0;
                }
            }
        }
        $result['invitation_rules'] = $invitation_rules;
        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }

    /**
     * 优惠券领取页面
     */
    public function coupon()
    {
        //$param = self::getHttpParam();

        $model = new InvitationMdl();
        $fields = 'id, invitation_coupon_id';
        $invitation = $model->field($fields)->find();

        // 优惠券信息
        $couponInfo = Coupon::where(['id' => $invitation['invitation_coupon_id']])->find();
        $couponInfo && $couponInfo = Coupon::formatCoupon($couponInfo);

        $data = [
            'couponInfo' => $couponInfo,
            // 背景图
            'invitation_bg' => 'https://' . $_SERVER['HTTP_HOST'] . '/public/static/wechat/image/invitation_bg.png'
        ];

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $data);
    }

    /**
     * 领取优惠券
     */
    public function receiveCoupon()
    {
        $param = self::getHttpParam();

        if (empty($param->couponId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '优惠券ID(couponId) 不能为空');
        }
        if (empty($param->phone)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '手机号(phone) 不能为空');
        }

        $memberId = Member::where('account_number', $param->phone)->value('id');
        if(!$memberId){
            return show(config('status.ERROR_STATUS'), '未找到该用户', '未找到该用户');
        }
        $data = [
            'id' => BaseHelper::getUUID(),
            'create_date' => date('Y-m-d H:i:s'),
            'member_id' => $memberId,
            'coupon_id' => $param->couponId
        ];

        $result = Membercoupon::insert($data);
        if ($result === false){
            return show(config('status.ERROR_STATUS'), '领取失败', '领取失败');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }

    //领取积分
    public function getIntegral()
    {
        $param = self::getHttpParam();
        $IntegralGetMdl = new IntegralGet();
        if (empty($param->phone)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'手机号为空');
        }

        $data = [
            'id' => BaseHelper::getUUID(),
            'phone' => $param->phone,
            'key' => $param->key
        ];
        $result = $IntegralGetMdl->save($data);
        if (!$result){
            return show(config('status.ERROR_STATUS'), '领取失败', '领取失败');
        }
        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }
}
